﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Controls
{
    public partial class UserListItem : BaseUserControl
    {
        public enum IsAlternate
        { 
            True,
            False
        }
        private IsAlternate _alternateRow = IsAlternate.False;

        public IsAlternate AlternateRow
        {
            get { return _alternateRow; }
            set { _alternateRow = value; }
        }

        public user User
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region print

        protected string PrintAlternate(string className = "alt-row")
        {
            return this.AlternateRow == IsAlternate.True ? className : " ";
        }

        protected string PrintUserName()
        {
            if (User != null)
                return String.Format("{0}", this.User.username);
            else
                return "n/a";
        }

        protected string PrintUserEmail()
        {
            if (User != null)
                return String.Format("{0}", this.User.email);
            else
                return "n/a";
        }

        protected string PrintUserDraws()
        {
            if (User != null)
                return String.Format("{0}", this.BasePage.BusinessFacade.PlayerBL.GetDrawsByPlayerId(User.id).Count());
            else
                return "0";
        }

        protected string PrintUserFunds()
        {
            if (User != null)
                return String.Format("&pound;{0:0.00}", this.User.funds);
            else
                return "&pound;0.00";
        }

        protected string PrintUserMoneySpent()
        {
            if (User != null)
                return String.Format("&pound;{0:0.00}", GetMoneySpend(User.id));
            else
                return "&pound;0.00";
        }

        private double GetMoneySpend(int id)
        {
            var draws = this.BasePage.BusinessFacade.PlayerBL.GetDrawsByPlayerId(id);

            var fundsUsed = 0;

            foreach (var draw in draws)
            {
                if (!draw.freeDraw)
                {
                    if ((int) draw.pricePerLine == 0)
                    {
                        switch (draw.type)
                        {
                            case draw.drawType.EUROMILLIONS:
                                fundsUsed += 2;
                                break;
                            default:
                                fundsUsed += 1;
                                break;
                        }
                    }
                    else
                    {
                        fundsUsed += (int)draw.pricePerLine;
                    }
                }
            }


            return fundsUsed;
        }

        

        protected string PrintFundsStatusClass()
        {
            if (User != null)
                return String.Format("{0}", this.User.funds > 0 ? "funds-green" : "funds-red");
            else
                return "";
        }

        protected string PrintUserIsAdmin()
        {
            if (User != null)
                return this.User.isAdmin ? "True" : "False";
            else
                return "n/a";
        }

        protected string PrintUserDetailsLink(string text = "view details")
        {
            if (User != null)
                return String.Format("<a href=\"/Secured/Users/Default.aspx?uId={0}\">{1}", User.id,text);
            else
                return "n/a";
        }

        #endregion
    }
}